package com.jianzhi.job.service.impl;
import com.jianzhi.job.service.XyhJzJobServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import com.jianzhi.job.entity.XyhJzJobEntity;
import com.jianzhi.price.entity.XyhJzPricelistEntity;
import com.jianzhi.step.entity.XyhJzStepEntity;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import java.util.ArrayList;
import java.util.UUID;
import java.io.Serializable;


@Service("xyhJzJobService")
@Transactional
public class XyhJzJobServiceImpl extends CommonServiceImpl implements XyhJzJobServiceI {
	
 	public <T> void delete(T entity) {
 		super.delete(entity);
 		//执行删除操作配置的sql增强
		this.doDelSql((XyhJzJobEntity)entity);
 	}
	
	public void addMain(XyhJzJobEntity xyhJzJob,
	        List<XyhJzPricelistEntity> xyhJzPricelistList,List<XyhJzStepEntity> xyhJzStepList){
			//保存主信息
			this.save(xyhJzJob);
		
			/**保存-兼职价格*/
			for(XyhJzPricelistEntity xyhJzPricelist:xyhJzPricelistList){
				//外键设置
				xyhJzPricelist.setJobId(xyhJzJob.getId());
				this.save(xyhJzPricelist);
			}
			/**保存-兼职步骤*/
			for(XyhJzStepEntity xyhJzStep:xyhJzStepList){
				//外键设置
				xyhJzStep.setJobId(xyhJzJob.getId());
				this.save(xyhJzStep);
			}
			//执行新增操作配置的sql增强
 			this.doAddSql(xyhJzJob);
	}

	
	public void updateMain(XyhJzJobEntity xyhJzJob,
	        List<XyhJzPricelistEntity> xyhJzPricelistList,List<XyhJzStepEntity> xyhJzStepList) {
		//保存主表信息
		this.saveOrUpdate(xyhJzJob);
		//===================================================================================
		//获取参数
		Object id0 = xyhJzJob.getId();
		Object id1 = xyhJzJob.getId();
		//===================================================================================
		//1.查询出数据库的明细数据-兼职价格
	    String hql0 = "from XyhJzPricelistEntity where 1 = 1 AND jOB_ID = ? ";
	    List<XyhJzPricelistEntity> xyhJzPricelistOldList = this.findHql(hql0,id0);
		//2.筛选更新明细数据-兼职价格
		if(xyhJzPricelistList!=null&&xyhJzPricelistList.size()>0){
		for(XyhJzPricelistEntity oldE:xyhJzPricelistOldList){
			boolean isUpdate = false;
				for(XyhJzPricelistEntity sendE:xyhJzPricelistList){
					//需要更新的明细数据-兼职价格
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-兼职价格
		    		super.delete(oldE);
	    		}
	    		
			}
			//3.持久化新增的数据-兼职价格
			for(XyhJzPricelistEntity xyhJzPricelist:xyhJzPricelistList){
				if(oConvertUtils.isEmpty(xyhJzPricelist.getId())){
					//外键设置
					xyhJzPricelist.setJobId(xyhJzJob.getId());
					this.save(xyhJzPricelist);
				}
			}
		}
		//===================================================================================
		//1.查询出数据库的明细数据-兼职步骤
	    String hql1 = "from XyhJzStepEntity where 1 = 1 AND jOB_ID = ? ";
	    List<XyhJzStepEntity> xyhJzStepOldList = this.findHql(hql1,id1);
		//2.筛选更新明细数据-兼职步骤
		if(xyhJzStepList!=null&&xyhJzStepList.size()>0){
		for(XyhJzStepEntity oldE:xyhJzStepOldList){
			boolean isUpdate = false;
				for(XyhJzStepEntity sendE:xyhJzStepList){
					//需要更新的明细数据-兼职步骤
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-兼职步骤
		    		super.delete(oldE);
	    		}
	    		
			}
			//3.持久化新增的数据-兼职步骤
			for(XyhJzStepEntity xyhJzStep:xyhJzStepList){
				if(oConvertUtils.isEmpty(xyhJzStep.getId())){
					//外键设置
					xyhJzStep.setJobId(xyhJzJob.getId());
					this.save(xyhJzStep);
				}
			}
		}
		//执行更新操作配置的sql增强
 		this.doUpdateSql(xyhJzJob);
	}

	
	public void delMain(XyhJzJobEntity xyhJzJob) {
		//删除主表信息
		this.delete(xyhJzJob);
		//===================================================================================
		//获取参数
		Object id0 = xyhJzJob.getId();
		Object id1 = xyhJzJob.getId();
		//===================================================================================
		//删除-兼职价格
	    String hql0 = "from XyhJzPricelistEntity where 1 = 1 AND jOB_ID = ? ";
	    List<XyhJzPricelistEntity> xyhJzPricelistOldList = this.findHql(hql0,id0);
		this.deleteAllEntitie(xyhJzPricelistOldList);
		//===================================================================================
		//删除-兼职步骤
	    String hql1 = "from XyhJzStepEntity where 1 = 1 AND jOB_ID = ? ";
	    List<XyhJzStepEntity> xyhJzStepOldList = this.findHql(hql1,id1);
		this.deleteAllEntitie(xyhJzStepOldList);
	}
	
 	
 	/**
	 * 默认按钮-sql增强-新增操作
	 * @param id
	 * @return
	 */
 	public boolean doAddSql(XyhJzJobEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-更新操作
	 * @param id
	 * @return
	 */
 	public boolean doUpdateSql(XyhJzJobEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-删除操作
	 * @param id
	 * @return
	 */
 	public boolean doDelSql(XyhJzJobEntity t){
	 	return true;
 	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @return
	 */
 	public String replaceVal(String sql,XyhJzJobEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{name}",String.valueOf(t.getName()));
 		sql  = sql.replace("#{stoptime}",String.valueOf(t.getStoptime()));
 		sql  = sql.replace("#{introduce}",String.valueOf(t.getIntroduce()));
 		sql  = sql.replace("#{sort}",String.valueOf(t.getSort()));
 		sql  = sql.replace("#{category_id}",String.valueOf(t.getCategoryId()));
 		sql  = sql.replace("#{checktime}",String.valueOf(t.getChecktime()));
 		sql  = sql.replace("#{lefttimes}",String.valueOf(t.getLefttimes()));
 		sql  = sql.replace("#{offshelf}",String.valueOf(t.getOffshelf()));
 		sql  = sql.replace("#{shanghu}",String.valueOf(t.getShanghu()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}
}